package com.tiyende.credit.loop.ui.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.os.CountDownTimer
import android.view.LayoutInflater
import android.view.View
import com.tiyende.credit.loop.R
import com.tiyende.credit.loop.common.CommonDialog
import com.tiyende.credit.loop.databinding.TiyendeDialogUserDataClearBinding
import com.tiyende.credit.loop.event.TiyendeEventPointType

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/27 15:50
 * @Annotation：清空用户数据dialog
 */
class TiyendeUserDataClearDialog: CommonDialog {

    private lateinit var binding: TiyendeDialogUserDataClearBinding
    private var mCallback: IDialogCallback? = null

    //等待时长
    private var countdownTimer: MyCountDownTimer? = null
    private var countdown = 10

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getTiyendeRootView(): View {
        binding = TiyendeDialogUserDataClearBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initTiyendeView() {

    }

    override fun initTiyendeListener() {
        setOnClickListener(binding.imgClose)
        setOnClickListener(binding.btnReturn)
        setOnClickListener(binding.btnConfirm)
    }

    override fun loadTiyendeData() {

    }

    override fun onTiyendeViewClick(v: View?) {
        dismiss()
        when(v?.id){
            R.id.btnConfirm -> {
                mCallback?.onUserDataClearConfirmClick()
            }
        }
    }

    interface IDialogCallback {
        //按钮点击
        fun onUserDataClearConfirmClick()
    }

    class MyCountDownTimer(private val dialog: TiyendeUserDataClearDialog, millisInFuture: Int, countDownInterval: Long): CountDownTimer(millisInFuture.toLong(), countDownInterval) {
        override fun onTick(millisUntilFinished: Long) {
            // 这里每隔一段时间（定时器间隔）会被调用
            // 可以在这里更新UI，显示剩余时间
            dialog.countdown = (millisUntilFinished / 1000).toInt()
            dialog.checkClearUserDataConfirmStatus()
        }
        override fun onFinish() {
            // 倒计时完成时被调用
            dialog.checkClearUserDataConfirmStatus()
        }
    }

    /**
     * 启动倒计时
     */
    @SuppressLint("SetTextI18n")
    private fun startCountdownTimer() {
        //随机 60秒 等待时长
        countdown = 10
        checkClearUserDataConfirmStatus()
        if (countdownTimer == null){
            countdownTimer = MyCountDownTimer(this, countdown * 1000, 1000)
        }
        countdownTimer?.start()
    }

    @SuppressLint("SetTextI18n")
    private fun checkClearUserDataConfirmStatus(){
        if (countdown <= 0){
            binding.btnConfirm.isEnabled = true
            binding.btnConfirm.text = getString(R.string.tiyende_button_confirm)
        } else {
            binding.btnConfirm.isEnabled = false
            binding.btnConfirm.text = "${getString(R.string.tiyende_button_confirm)}(${countdown}s)"
        }
    }

    override fun onStart() {
        super.onStart()
        startCountdownTimer()
        addEventPointRequestAction(TiyendeEventPointType.DIALOG.TIYENDE_DIALOG_USER_DATA_CLEAR_SHOW)
    }

    override fun dismiss() {
        super.dismiss()
        if (countdownTimer != null) {
            countdownTimer?.cancel()
            countdownTimer = null
        }
        addEventPointRequestAction(TiyendeEventPointType.DIALOG.TIYENDE_DIALOG_USER_DATA_CLEAR_DISMISS)
    }
}